package com.itboye.eweilisten.adapter;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.itboye.eweilisten.R;
import com.itboye.eweilisten.model.GoodsDetailBean;

import java.util.ArrayList;

/**
 * <br/>
 * <br/>
 * <p>
 * 二级 子类分组的适配器
 * <p>
 * <br/>
 * <br/>
 * <p>
 * 方法{@link #isChildSelectable(int, int)} <b><font color='#ff00ff'
 * size='2'>必须返回true</font></b>
 */
public class ChildAdapter extends BaseExpandableListAdapter {

    private Context mContext;// 上下文

    private ArrayList<GoodsDetailBean.Units.UnitTwos> mChilds;// 数据源
    private Boolean bought;

    public ChildAdapter(Context context, ArrayList<GoodsDetailBean.Units.UnitTwos> childs, Boolean bought) {
        this.mContext = context;
        this.mChilds = childs;
        this.bought = bought;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return mChilds.get(groupPosition).getUnits() != null ? mChilds
                .get(groupPosition).getUnits().size() : 0;
    }

    @Override
    public String getChild(int groupPosition, int childPosition) {
        if (mChilds.get(groupPosition).getUnits().get(childPosition) != null
                && mChilds.get(groupPosition).getUnits().size() > 0)
            return mChilds.get(groupPosition).getUnits()
                    .get(childPosition).getUnit_name();
        return null;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isExpanded, View convertView, ViewGroup parent) {
        ChildHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.child_child_item, null);
            holder = new ChildHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ChildHolder) convertView.getTag();
        }
        Log.d("getChildView", isExpanded + "");
        //第三季
        holder.update(getChild(groupPosition, childPosition));

        if (bought) {
            holder.lijitiyan.setVisibility(View.INVISIBLE);
        } else {
            if (mChilds.get(groupPosition).getUnits().get(childPosition).getIs_free().equals("1")) {
                holder.lijitiyan.setVisibility(View.VISIBLE);
            } else {
                holder.lijitiyan.setVisibility(View.INVISIBLE);
            }
        }

        return convertView;
    }

    /**
     * Holder优化
     */
    class ChildHolder {

        private TextView childChildTV;

        private TextView lijitiyan;

        public ChildHolder(View v) {
            childChildTV = (TextView) v.findViewById(R.id.childChildTV);
            lijitiyan = (TextView) v.findViewById(R.id.lijitiyan);
        }

        public void update(String str) {
            childChildTV.setText(str);
        }
    }

    @Override
    public Object getGroup(int groupPosition) {
        if (mChilds != null && mChilds.size() > 0)
            return mChilds.get(groupPosition);
        return null;
    }

    @Override
    public int getGroupCount() {
        return mChilds != null ? mChilds.size() : 0;
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        GroupHolder holder = null;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(
                    R.layout.child_group_item, null);
            holder = new GroupHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (GroupHolder) convertView.getTag();
        }
        Log.d("getGroupView", isExpanded + "");
        if (isExpanded) {
            holder.imgBoosRight.setBackgroundResource(R.drawable.ex_up);
        } else {
            holder.imgBoosRight.setBackgroundResource(R.drawable.ex_buuton);
        }
        //第二级
        holder.update(mChilds.get(groupPosition));
        return convertView;
    }

    /**
     * Holder优化
     */
    class GroupHolder {

        private TextView childGroupTV;
        private ImageView imgBoosRight;


        public GroupHolder(View v) {
            imgBoosRight = (ImageView) v.findViewById(R.id.imgBoosRight);
            childGroupTV = (TextView) v.findViewById(R.id.childGroupTV);
        }

        public void update(GoodsDetailBean.Units.UnitTwos model) {
            childGroupTV.setText(model.getUnit_name());
        }
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        /**
         * ==============================================
         * 此处必须返回true，否则无法响应子项的点击事件===============
         * ==============================================
         **/
        return true;
    }

}
